package com.ks.app.admin.service.impl;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Service;
import com.ks.app.admin.service.KsresolutionService;
import com.ks.app.admin.service.impl.BaseServiceImpl;
import com.ks.app.admin.hibernate.KsResolution;
import com.ks.app.admin.util.Pager;
import com.ks.app.admin.hibernate.KsOperaterLog;
import com.ks.app.admin.service.KsoperatelogService;

@Service("ksresolutionService")
public class KsresolutionServiceImpl extends BaseServiceImpl implements KsresolutionService{
	private EntityManager em;
	public EntityManager getEntityManager() {
			return em;
		}
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
			this.em = em;
		}
	@Resource
	private KsoperatelogService ksoperatelogService;
	private KsOperaterLog ksOperateLog;	
	
	public KsOperaterLog getKsOperateLog() {
		return ksOperateLog;
	}
	public void setKsOperateLog(KsOperaterLog ksOperateLog) {
		this.ksOperateLog = ksOperateLog;
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////ksResolution
//根据条件 分页展示所有分辨率数据
	public List<KsResolution> GetKsResolutionList(String isshow, Pager pager) {

		String sql = " from KsResolution where 1=1 ";		
		if(isshow!=null&&isshow.length()>0)
		{
			sql=sql+"  and isshow=? ";	
		}		
		Query query = em.createQuery(sql+" order by rsltid ");
		if(isshow!=null&&isshow.length()>0)
		{   			
			long mid=Long.parseLong(isshow);
			query.setParameter(1, mid);
		}
		query.setMaxResults(pager.getCntOfPage()) ;
	    query.setFirstResult( (pager.getCurPage()-1) * pager.getCntOfPage() );
	    pager.setTotoalCnt(GetcntKsResolution(isshow));
		@SuppressWarnings("unchecked")
		List <KsResolution> list = query.getResultList();	
		return list;
	}
	//根据状态获得分辨率
	public List<KsResolution> GetKsResolutionList(String isshow) 
	{

		String sql = " from KsResolution where 1=1 ";		
		if(isshow!=null&&isshow.length()>0)
		{
			sql=sql+"  and isshow=? ";	
		}		
		Query query = em.createQuery(sql+" order by rsltid ");
		if(isshow!=null&&isshow.length()>0)
		{   			
			long mid=Long.parseLong(isshow);
			query.setParameter(1, mid);
		}		
		@SuppressWarnings("unchecked")
		List <KsResolution> list = query.getResultList();	
		return list;
	}
	//获得记录行数
	public int GetcntKsResolution(String isshow) {
		// TODO Auto-generated method stub
		String sql = "from KsResolution where 1=1 ";
		if(isshow!=null&&isshow.length()>0)
		{
			sql=sql+" and isshow=? ";	
		}			
		Query query = em.createQuery(sql+" order by rsltid ");
		if(isshow!=null&&isshow.length()>0)
		{   				
			long mid=Long.parseLong(isshow);
			query.setParameter(1, mid);
		}
		return query.getResultList().size();
	}
	//根据ID 得到具体的分辨率数据
	public List<KsResolution> GetKsResolution(String uiid) {
		// TODO Auto-generated method stub
		String sql = " from KsResolution where 1=1  ";	
		if(uiid!=null&&uiid.length()>0)
		{
			sql=sql+" and rsltid=? ";	
		}		
		else
		{return null;}
		Query query = em.createQuery(sql+" order by rsltid ");
		if(uiid!=null&&uiid.length()>0)
		{   			
			long mid=Long.parseLong(uiid);
			query.setParameter(1, mid);
		}	   
		@SuppressWarnings("unchecked")
		List <KsResolution> list = query.getResultList();	
		return list;
	}
	//判断分辨率是否重复
	public List<KsResolution> GetKsKsResolutionByResol(String resolution,String Uid) {
		// TODO Auto-generated method stub
		String sql = " from KsResolution  where 1=1 ";		
		if(resolution!=null&&resolution.length()>0)
		{
			sql=sql+" and resolution=? ";	
		}		
		else 
		{return null;}
		if(Uid!=null&&Uid.length()>0)
		{
			sql=sql+" and rsltid!='"+Uid+"' ";	
		}	
		Query query = em.createQuery(sql+" order by rsltid ");
		if(resolution!=null&&resolution.length()>0)
		{   
			query.setParameter(1, resolution);
		}	   
		@SuppressWarnings("unchecked")
		List <KsResolution> list = query.getResultList();	
		return list;
	}
	//添加分辨率
	public long addKsResolution(KsResolution Ksresolution,String username) {

		try
		{			
			//添加日志
			String Resolution=Ksresolution.getResolution();
			long show=Ksresolution.getIsshow();
			em.persist(Ksresolution);
			long res=Ksresolution.getRsltid();
			ksOperateLog =new KsOperaterLog();
			ksOperateLog.setContent(username+"添加了分辨率:[id="+res+"];[分辨率="+Resolution+"];[Isshow="+show+"]；");
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(username);
			ksOperateLog.setLogtype(1);
			ksOperateLog.setStatus(2);
			ksoperatelogService.save(ksOperateLog);
			return res;
		}
		catch(Exception ee)
		{  
			//ee.printStackTrace();
			return 0;
		}
	}
	//修改分辨率
	public long updateKsResolution(KsResolution Ksresolution,String username) {

		try
		{				
			//添加修改日志
			String Resolution=Ksresolution.getResolution();
			long show=Ksresolution.getIsshow();
			long uid=Ksresolution.getRsltid();			
			em.merge(Ksresolution);		
			
			ksOperateLog =new KsOperaterLog();
			ksOperateLog.setContent(username+"修改了分辨率:[id="+uid+"];[分辨率="+Resolution+"];[Isshow="+show+"]；");
			ksOperateLog.setLogtime(new Date());
			ksOperateLog.setOperater(username);
			ksOperateLog.setLogtype(1);
			ksOperateLog.setStatus(2);
			ksoperatelogService.save(ksOperateLog);
			return 1;
		}
		catch(Exception ee)
		{
			//System.out.print(ee.toString());
			return 0;
		}
	}
	//删除分辨率
	public int deleteKsResolution(String uiid,String username) {

		try
		{			
			if(uiid!=null&&uiid.length()>0)
			{
				String sql = " from KsResolution where rsltid=? ";	
				Query query = em.createQuery(sql);			
				long m_iid=Long.parseLong(uiid);
				query.setParameter(1,m_iid);			
				@SuppressWarnings("unchecked")
				List <KsResolution> list = query.getResultList();		
				if(list.size()>0)
				{
					em.remove(list.get(0));	
					String Resolution=list.get(0).getResolution();
					long show=list.get(0).getIsshow();
					long uid=list.get(0).getRsltid();	
					ksOperateLog =new KsOperaterLog();
					ksOperateLog.setContent(username+"删除了分辨率:[id="+uid+"];[分辨率="+Resolution+"];[Isshow="+show+"]；");
					ksOperateLog.setLogtime(new Date());
					ksOperateLog.setOperater(username);
					ksOperateLog.setLogtype(1);
					ksOperateLog.setStatus(2);
					ksoperatelogService.save(ksOperateLog);
					return 1;
				}
			}	
			return 0;
		}
		catch(Exception ee)
		{return 0;}
	}
}
